package com.czy.owner.ui;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.HotCarProductAdapter;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.entity.HotCarProductEntity;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2017/11/3.
 */

public class HotCarProductActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView easyRecyclerView;
    private HotCarProductAdapter adapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("热门车品");
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(HotCarProductActivity.this));
        easyRecyclerView.addItemDecoration(new RecycleViewDivider(HotCarProductActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        easyRecyclerView.setAdapterWithProgress(adapter = new HotCarProductAdapter(HotCarProductActivity.this));
        easyRecyclerView.setRefreshListener(this);
        onRefresh();
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<HotCarProductEntity>() {
            @Override
            public void onItemClick(View view, int position, HotCarProductEntity data) {
//                int currentCount = data.getCurrentCount();
//                if (currentCount != 0) {
                Intent intent = new Intent(HotCarProductActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", data.getGoodsId());
                intent.putExtra("mFromType",Constants.GOODSSOURCE_STORE);
                intent.putExtra("mFromTypeId", "0");
                startActivity(intent);
//                }else {
//                    PhoneUtils.ShowToastMessage(HotCarProductActivity.this,"该商品已售罄");
//                }

            }
        });
    }

    @Override
    protected void updateViews() {

    }

    private void getData() {
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_SHOP_HOTGOODSLIST;
        RequestParams params = new RequestParams(url);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess==" + result);
                String data = PhoneUtils.checkResponseFlag(HotCarProductActivity.this, result);
                if (data != null) {
                    Gson gson = new GsonBuilder().create();
                    List<HotCarProductEntity> dataList = gson.fromJson(data, new TypeToken<List<HotCarProductEntity>>() {
                    }.getType());
                    adapter.addAll(dataList);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError==" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });
    }

    @Override
    public void onRefresh() {
        adapter.clear();
        getData();

    }
}
